//
//  YXJView.m
//  Piece
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "YXJView.h"
#import "YXJMusicPlayController.h"
#import "AudioPlayerManager.h"
@implementation YXJView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.cornerRadius = 5;
        
        self.userImageView = [[UIImageView alloc] init];
        self.userImageView.layer.cornerRadius = 20;
        self.userImageView.layer.masksToBounds = YES;
        [self addSubview:_userImageView];
        
        self.realName = [[UILabel alloc] init];
        [self.realName setTextColor:[UIColor colorWithRed:0.58 green:0.76 blue:0.91 alpha:1.00]];
        self.realName.font = [UIFont systemFontOfSize:14];;
        [self addSubview:_realName];
        
        self.nickName = [[UILabel alloc] init];
        self.nickName.numberOfLines = 0;
        self.nickName.font = [UIFont systemFontOfSize:13];
        [self.nickName setTextColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.00]];
        [self addSubview:_nickName];
        
        self.musicName = [[UILabel alloc] init];
        [self addSubview:_musicName];
        
        self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.isPlay = NO;
        [self.playButton setImage:[UIImage imageNamed:@"play_normal"] forState:UIControlStateNormal];
        [self.playButton addTarget:self action:@selector(didClickedPlay:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_playButton];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.font = [UIFont systemFontOfSize:13];
        [self.dateLabel setTextColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.00]];
        [self addSubview:_dateLabel];
        
       
    }
    return self;
}

- (void)didClickedPlay:(UIButton *)sender
{
    if (self.isPlay == NO) {
        [sender setImage:[UIImage imageNamed:@"pause_normal"] forState:UIControlStateNormal];
        _clickBlock();
        
        self.isPlay = YES;
        [self.delegate musicPlay];
    }else
    {
        [sender setImage:[UIImage imageNamed:@"play_normal"] forState:UIControlStateNormal];
        self.isPlay = NO;
        AudioPlayerManager *playerManager = [AudioPlayerManager shareAudioPlayerManager];
//        playerManager.delegate = self;
        [playerManager stop];
        
        [[[XZHMusicPlayView shareMusicPlayViewInstance] pauseButton] setHidden:YES];
        [[[XZHMusicPlayView shareMusicPlayViewInstance] playButton] setHidden:NO];
        [[[XZHMusicPlayView shareMusicPlayViewInstance] titleLabel] setText:@"PIECE"];
        [[[XZHMusicPlayView shareMusicPlayViewInstance] titleLabel] setFont:[UIFont systemFontOfSize:20]];
    }
}

-(void)layoutSubviews
{
    self.userImageView.frame = CGRectMake(10, 10, 40, 40);
    
    self.realName.frame = CGRectMake(_userImageView.frame.origin.x + _userImageView.frame.size.width + 10, _userImageView.frame.origin.y, 200, 20);
    
    self.nickName.frame = CGRectMake(_realName.frame.origin.x, _realName.frame.origin.y + _realName.frame.size.height - 10, _realName.frame.size.width + 10, (_realName.frame.size.height) * 2);
    
    self.musicName.frame = CGRectMake(_userImageView.frame.origin.x, _userImageView.frame.origin.y + _userImageView.frame.size.height + 20, 250, 30);
    
    self.playButton.frame = CGRectMake(self.frame.size.width - 50, 40, 40, 40);
    
    self.playImageView.frame = self.playButton.bounds;
    

    self.dateLabel.frame = CGRectMake(self.frame.size.width - 90, _playButton.frame.origin.y + _playButton.frame.size.height + 10, 80, 20);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"index" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playNotificationAciton:) name:@"play" object:nil];



    self.dateLabel.frame = CGRectMake(self.frame.size.width - 85, _playButton.frame.origin.y + _playButton.frame.size.height + 5, 80, 20);

}

- (void)setOtherNum:(NSInteger)otherNum
{
    if (_otherNum != otherNum) {
        _otherNum = otherNum;
    }
}

- (void)notificationAction:(NSNotification *)notification
{
    if (self.otherNum == [notification.object integerValue]) {
        [self.playButton setImage:[UIImage imageNamed:@"pause_normal"] forState:UIControlStateNormal];
        self.isPlay = YES;
    } else {
        [self.playButton setImage:[UIImage imageNamed:@"play_normal"] forState:UIControlStateNormal];
        self.isPlay = NO;
    }
}

- (void)playNotificationAciton:(NSNotification *)notification
{
    if ([notification.object integerValue] == 1) {
        [self.playButton setImage:[UIImage imageNamed:@"play_normal"] forState:UIControlStateNormal];
        self.isPlay = NO;
    }
}
    



@end
